//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmStepConditionComposite.h"
using namespace Cephei::QL::Methods::Finitedifferences::Stepconditions;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Stepconditions/FdmSnapshotCondition.h>
#include <gen/QL/Cashflows/Dividend.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmInnerValueCalculator.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (boost::shared_ptr<QuantLib::FdmStepConditionComposite>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	_ppFdmStepConditionComposite = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::FdmStepConditionComposite& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	_ppFdmStepConditionComposite = new boost::shared_ptr<QuantLib::FdmStepConditionComposite> (&childNative);
    
    _FdmStepConditionCompositeOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (CFdmStepConditionComposite^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmStepConditionComposite = new boost::shared_ptr<QuantLib::FdmStepConditionComposite> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	if (!t->IsSubclassOf(CFdmStepConditionComposite::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::Handle<QuantLib::FdmStepConditionComposite>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmStepConditionComposite = &childNative;
	_ppFdmStepConditionComposite = &static_cast<boost::shared_ptr<QuantLib::FdmStepConditionComposite>>(childNative.currentLink());
    
    _FdmStepConditionCompositeOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::Handle<QuantLib::FdmStepConditionComposite> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmStepConditionComposite = &childNative;
	_ppFdmStepConditionComposite = &static_cast<boost::shared_ptr<QuantLib::FdmStepConditionComposite>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::FdmStepConditionComposite childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	_ppFdmStepConditionComposite = new boost::shared_ptr<QuantLib::FdmStepConditionComposite> (new QuantLib::FdmStepConditionComposite (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::~CFdmStepConditionComposite ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmStepConditionComposite != NULL)
    {
	    delete _ppFdmStepConditionComposite;
        _ppFdmStepConditionComposite = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::!CFdmStepConditionComposite ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmStepConditionComposite != NULL)
    {
	    delete _ppFdmStepConditionComposite;
    }
}
QuantLib::FdmStepConditionComposite& Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::GetReference ()
{
    if (_ppFdmStepConditionComposite == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmStepConditionComposite;
}
boost::shared_ptr<QuantLib::FdmStepConditionComposite>& Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::GetShared ()
{
    if (_ppFdmStepConditionComposite == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmStepConditionComposite;
}
QuantLib::FdmStepConditionComposite* Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::GetPointer ()
{
    if (_ppFdmStepConditionComposite == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmStepConditionComposite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmStepConditionComposite>& Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::GetHandle ()
{
	if (_phFdmStepConditionComposite == NULL)
	{
		_phFdmStepConditionComposite = new Handle<QuantLib::FdmStepConditionComposite> (*_ppFdmStepConditionComposite);
	}
	return *_phFdmStepConditionComposite;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::HasNative () 
{
	return (_ppFdmStepConditionComposite != NULL);
}

Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmStepConditionComposite^ Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	(*_ppFdmStepConditionComposite)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmStepConditionComposite^ Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::JoinConditions (Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmSnapshotCondition^ c1, Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmStepConditionComposite^ c2)
{
    CFdmSnapshotCondition^ _Cc1;
    CFdmStepConditionComposite^ _Cc2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cc1 = safe_cast<CFdmSnapshotCondition^> (c1);
        _Cc1->Lock();
        boost::shared_ptr<QuantLib::FdmSnapshotCondition>& _c1 = static_cast<boost::shared_ptr<QuantLib::FdmSnapshotCondition>&> (_Cc1->GetShared ()); 
        _Cc2 = safe_cast<CFdmStepConditionComposite^> (c2);
        _Cc2->Lock();
        boost::shared_ptr<QuantLib::FdmStepConditionComposite>& _c2 = static_cast<boost::shared_ptr<QuantLib::FdmStepConditionComposite>&> (_Cc2->GetShared ()); 
    	boost::shared_ptr<QuantLib::FdmStepConditionComposite> _rv = (boost::shared_ptr<QuantLib::FdmStepConditionComposite>)(*_ppFdmStepConditionComposite)->joinConditions ( _c1,  _c2 );   
        Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc1 != nullptr) _Cc1->Unlock();
        if (_Cc2 != nullptr) _Cc2->Unlock();
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::StoppingTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppFdmStepConditionComposite)->stoppingTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmStepConditionComposite^ Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite::VanillaComposite (Cephei::Core::IVector<Cephei::QL::Cashflows::IDividend^>^ schedule, Cephei::QL::IExercise^ exercise, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calculator, DateTime refDate, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    CExercise^ _Cexercise;
    CFdmMesher^ _Cmesher;
    CFdmInnerValueCalculator^ _Ccalculator;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        schedule->Lock();
        INativeVector<Cephei::QL::Cashflows::IDividend^>^ _NCIschedule = schedule->getFeature (NativeFeature::shared_ptr);
        CDividendVector^ _NCschedule = safe_cast<CDividendVector^>(_NCIschedule);
        std::vector<boost::shared_ptr<QuantLib::Dividend> >& _schedule = static_cast<std::vector<boost::shared_ptr<QuantLib::Dividend> >&> (_NCschedule->GetShared ());
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _Ccalculator = safe_cast<CFdmInnerValueCalculator^> (calculator);
        _Ccalculator->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _calculator = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_Ccalculator->GetShared ()); 
        QuantLib::Date _refDate = (QuantLib::Date)ValueHelper::Convert (refDate); //a
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
    	boost::shared_ptr<QuantLib::FdmStepConditionComposite> _rv = (boost::shared_ptr<QuantLib::FdmStepConditionComposite>)(*_ppFdmStepConditionComposite)->vanillaComposite ( _schedule,  _exercise,  _mesher,  _calculator,  _refDate,  _dayCounter );   
        Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmStepConditionComposite (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (schedule != nullptr) schedule->Unlock(); //NO2  
        if (_Cexercise != nullptr) _Cexercise->Unlock();
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_Ccalculator != nullptr) _Ccalculator->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

